package com.ymatou.shop.ui.activity;

import com.google.gson.JsonObject;
import com.ymatou.shop.R;
import com.ymatou.shop.api.ApiRequest;
import com.ymatou.shop.api.ApiResponse;
import com.ymatou.shop.cache.RequestInfo;
import com.ymatou.shop.cache.exception.CacheException;
import com.ymatou.shop.model.ResponseMessage;
import com.ymatou.shop.util.DialogUtil;
import com.ymatou.shop.util.GlobalUtil;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.SlidingDrawer;

public class CheckCodeActivity extends BaseActivity {
	
	private EditText checkCodeEditText;
	private Button submitButton;
	private ResponseMessage responseMessage; 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.check_code_activity);
		
		setupViews();
	}
	
	@Override
	protected void setupViews() {
		
		
		checkCodeEditText = (EditText)findViewById(R.id.checkcode);
		submitButton = (Button)findViewById(R.id.submit);
		
		checkCodeEditText.setText("05d258141a68eadf");
		
		submitButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String checkCode = checkCodeEditText.getText().toString();
				RequestInfo request = ApiRequest.invitationCode(checkCode);
				getCacheManager().register(0, request, CheckCodeActivity.this);
			}
		});
		
		super.setupViews();
	}
	
	
	@Override
	public void onCacheSuccess(int requestId, RequestInfo requestInfo, Object response) {
		responseMessage = ApiResponse.invitationCode((String)response); 
		if (responseMessage.status == 200){
			setResult(Activity.RESULT_OK);
			finish();
		}
	  super.onCacheSuccess(requestId, requestInfo, response);
	}
	
	@Override
	public void onCacheFailed(int requestId, RequestInfo requestInfo, CacheException error) {
		if (requestId == 0){
			responseMessage = ApiResponse.invitationCode((String)error.getOrgResponse()); 
			DialogUtil.toast(this, responseMessage.message);
		}else {
			super.onCacheFailed(requestId, requestInfo, error);	
		}
	  
	}
}
